set more off

******************************************************************
use "Raw Data/results_10states2009_clean.dta", clear
drop if ac_num<0 //dropping postal ballot
drop if cand_party == "IND"

merge m:1 state_name ac_num using "Analysis Data/AC_est_sample.dta", keep(matched) nogen keepusing(treat wgt_treat pc_num)

keep state_name ac_num pc_num cand_votes total_ac_votes cand_party treat wgt_treat
rename cand_votes cand_votes_2009
rename total_ac_votes total_ac_votes_2009

tempfile temp1
save `temp1', replace

use "Raw Data/results_10states2014_clean.dta", clear
drop if ac_num<0 //dropping postal ballot

*Number of candidates
gen ind = (cand_party=="IND")
bys state_name ac_num: egen num_ind = sum(ind)

gen cand= 1
bys state_name ac_num: egen num_cand = sum(cand)

gen num_party = num_cand-num_ind

drop num_ind cand ind

drop if cand_party == "IND"

merge m:1 state_name ac_num using "Analysis Data/AC_est_sample.dta", keep(matched) nogen keepusing(treat wgt_treat pc_num)

keep state_name ac_num pc_num cand_votes total_ac_votes cand_party treat wgt_treat nota_votes num_cand num_party

rename cand_votes cand_votes_2014
rename total_ac_votes total_ac_votes_2014
rename nota_votes nota_votes_2014

merge 1:1 state_name ac_num cand_party using `temp1', keep(matched master) nogen

bys state_name ac_num: egen x = min(total_ac_votes_2009)
replace total_ac_votes_2009=x if total_ac_votes_2009==.
drop x

replace cand_votes_2009= 0 if cand_votes_2009==.

*Coding alliances
replace cand_party = "NDA" if cand_party == "BJP"
replace cand_party = "NDA" if cand_party == "TDP" & state_name=="Andhra Pradesh"
replace cand_party = "NDA" if cand_party == "SHS" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "RSPS" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "RPI(A)" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "SWP" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "LJP" & state_name=="Bihar"
replace cand_party = "NDA" if cand_party == "BLSP" & state_name=="Bihar"
replace cand_party = "NDA" if cand_party == "AD" & state_name=="Uttar Pradesh"

replace cand_party = "UPA" if inlist(cand_party,"INC","RJD","BVA","MD")
replace cand_party = "UPA" if cand_party == "JMM" & state_name == "Jharkhand"
replace cand_party = "UPA" if cand_party == "NCP" & state_name == "Maharashtra"
replace cand_party = "UPA" if cand_party == "RLD" & state_name == "Uttar Pradesh"

duplicates tag state_name pc_num ac_num cand_party, gen(dup)
tab dup
drop dup

gen voteshare_2014 = 100*cand_votes_2014/total_ac_votes_2014
gen voteshare_2009 = 100*cand_votes_2009/total_ac_votes_2009

save "Analysis Data/voteshare_data.dta", replace
************************************